<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Start of file printing.php */
/* Location: ./billing-ar/controllers/printing.php */
class Printing extends CI_Controller {
	
	function __construct(){
		parent::__construct();
        $this->load->model('customers'); /** load table customer **/ 
        $this->load->model('invoices'); /** load table item invoice **/ 
        $this->load->model('gas'); /** load table gas **/ 
        if(!$this->session->userdata('username')) /** restrict area if not acces to login page**/
			redirect('/',301); 
    }
    
    function invoice(){
        $this->ws->set('title',lang('invoice'));/** title page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $this->ws->set('customers',$this->customers->getCustomerData()); /** table customer */
        if($this->session->userdata('invoice_month')):
            $month   =  $this->session->userdata('invoice_month');
            $year    =  $this->session->userdata('invoice_year');
            $cust    =  $this->session->userdata('invoice_cust');
            $curr    =  $this->session->userdata('invoice_curr');
        else:
            $month   = date('m');
            $year    = date('Y');
            $cust    = '-'; 
            $curr    = 'IDR'; 
        endif;
        $val = $this->invoices->getItemInvoice($cust,$month,$year); /** table invoice */
        if($val):
            if($curr=="IDR"):
            	$invoice = $val['NoInvIDR'];
                $id      = $val['MataAnggaran'];
            	$meter   = $val['AmountIDR'];
            	$total   = 'Rp. '.number_format($val['AmountIDR'],0,".",",");
            	$calc    = IndonesianNumericToWord($meter);
            elseif($curr=="USD"):
            	$invoice = $val['NoInvUSD'];
            	$meter   = $val['AmountUSD'];
            	$total   = 'US$. '.number_format($val['AmountUSD'],0,".",",");
            	$calc    = EnglishNumericToWord($meter);      
            endif;
            $id       = $val['MataAnggaran'];
            $customer = $val['Perusahaan']; 
            $address  = $val['Alamat_1'].' '.$val['Alamat_2'].' '.$val['Alamat_3']; 
            $telephone= $val['Telephone']; 
            $fax      = $val['Fax'];
            $inv_date = $val['InvoiceDate'];
            $due_date = $val['DueDate'];
            $ref      = $val['Ref'];
            $pjbg     = $this->invoices->getPjbg($val['InvoiceID']);
            $metering = $this->invoices->getMetering('',$month,$year,$pjbg['pjbg_id']);
            $t_use    = $this->invoices->getSumMetering('',$month,$year,$pjbg['pjbg_id']);
            $avg      = $this->invoices->getAvgGhv($val['InvoiceID']);
            $line     = $this->invoices->getMeteringLine($curr,$pjbg['pjbg_id'],$val['InvoiceID']);
        endif;
        if($val):
            $this->ws->set('invoice',$invoice); /** invoice session **/ 
            $this->ws->set('inv_date',$inv_date); /** invoice session **/
            $this->ws->set('dueDate',$due_date); /** invoice due date session session **/
            $this->ws->set('cust_id',$id); /** cust id session **/ 
            $this->ws->set('cust_name',$customer); /** name session **/ 
            $this->ws->set('cust_addr',$address); /** address session **/ 
            $this->ws->set('cust_tlp',$telephone); /** telp session **/ 
            $this->ws->set('cust_fax',$fax); /** fax session **/ 
            $this->ws->set('cust_ref',$ref); /** invoice session **/
            $this->ws->set('metering',$metering); /** Metering model function getPBJG **/
            $this->ws->set('mtr_use',$t_use); /** Total SUM Metering model function getPBJG **/
            $this->ws->set('mtr_line',$line); /** Total USD/IR Metering Line **/ 
        endif;    
        $this->ws->set('month',$month); /** month session **/
        $this->ws->set('year',$year); /** year session **/
        $this->ws->set('cust',$cust); /** customer session **/
        $this->ws->set('curr',$curr); /** currency session **/
        $this->ws->set_theme('smooth'); /** set theme to smooth **/
		$this->ws->view('printing/invoice'); /** view layout to views/printing/invoice **/   
    }
    
    function getInvoice(){
        /** post variabel**/
        $month   =  $this->input->post('month');
        $year    =  $this->input->post('year');
        $cust    =  $this->input->post('customer');
        $curr    =  $this->input->post('curr');
        $value   =  array('invoice_month'=>$month,'invoice_year'=>$year,'invoice_cust'=>$cust,'invoice_curr'=>$curr); /** insert post to array value **/
        $this->session->set_userdata($value); /** insert array of value to session **/
        redirect('billing-ar/printing/invoice',301); /** redirect to receipt function **/
    }    
    
    function getPdfInvoice(){
        /** Session Var **/
        $month   =  $this->session->userdata('invoice_month');
        $year    =  $this->session->userdata('invoice_year');
        $cust    =  $this->session->userdata('invoice_cust');
        $curr    =  $this->session->userdata('invoice_curr');
            
        $val = $this->invoices->getItemInvoice($cust,$month,$year); /** table invoice */
        if($val):
            if($curr=="IDR"):
            	$invoice = $val['NoInvIDR'];
                $id      = $val['MataAnggaran'];
            	$meter   = $val['AmountIDR'];
            	$total   = 'Rp. '.number_format($val['AmountIDR'],0,".",",");
            	$calc    = IndonesianNumericToWord($meter);
            elseif($curr=="USD"):
            	$invoice = $val['NoInvUSD'];
            	$meter   = $val['AmountUSD'];
            	$total   = 'US$. '.number_format($val['AmountUSD'],0,".",",");
            	$calc    = EnglishNumericToWord($meter);
            endif;
            $id       = $val['MataAnggaran'];
            $number   = $val['Nomor'];
            $date     = $val['Tanggal'];
            $customer = $val['Perusahaan']; 
            $addr1    = $val['Alamat_1'];  
            $addr2    = $val['Alamat_2'];  
            $addr3    = $val['Alamat_3'];  
            $address  = $val['Alamat_1'].' '.$val['Alamat_2'].' '.$val['Alamat_3']; 
            $telephone= $val['Telephone']; 
            $fax      = $val['Fax'];
            $inv_date = $val['InvoiceDate'];
            /** explode Ref **/
            $tmp_ref  = $val['Ref'];
            $exp_ref  = explode(" ",$tmp_ref);
            $ref_1  = $exp_ref[0];
            $jml_1  = strlen($ref_1);
            $jml    = strlen($tmp_ref);
            
            $ref_2  = "";
            for($x=$jml_1;$x<=$jml;$x++):
                if(!empty($x)):
                    if(!$ref_2):
                	   $ref_2=$ref_2.$tmp_ref[$x];
                    endif;
                endif;
            endfor;
            
            $pjbg     = $this->invoices->getPjbg($val['InvoiceID']);
            $metering = $this->invoices->getMetering('',$month,$year,$pjbg['pjbg_id']);
            $t_use    = $this->invoices->getSumMetering('',$month,$year,$pjbg['pjbg_id']);
            $avg      = $this->invoices->getAvgGhv($val['InvoiceID']);
            $line     = $this->invoices->getMeteringLine($curr,$pjbg['pjbg_id'],$val['InvoiceID']);
        endif;
        
        $pdf = tcpdf();
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $k=0;
        $pdf->AddPage(); 
        /** center of ellipse **/
        $xc=202;
        $yc=48;
        $style7 = array('width' => 0.2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(200, 200, 200));
        /** X Y axis **/
        $pdf->SetDrawColor(200, 200, 200);
        $pdf->Line(7, $yc, $xc, $yc, $style7);
        
        /** initialization of x & y axis **/
        $x_axis_initial = 7;
        $y_axis_initial = 40;
        $x_axis = $x_axis_initial;
        $y_axis = $y_axis_initial; 
        
        $style4 = array('L' => 0,
                        'T' => array('width' => 0.25, 'cap' => 'butt', 'join' => 'miter', 'dash' => '20,10', 'phase' => 10, 'color' => array(100, 100, 255)),
                        'R' => array('width' => 0.50, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => array(50, 50, 127)),
                        'B' => array('width' => 0.75, 'cap' => 'square', 'join' => 'miter', 'dash' => '30,10,5,10'));
        $pdf->SetFont('helvetica', '', 14);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetDrawColor(0, 0, 0);
        $pdf->SetLineWidth(0.3); 
        
        $y_axis0=$y_axis+7; 
        $y_axis=$y_axis;
        
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial);
        
        if($val['Approval']==0) $appr="DRAFT"; else $appr="INVOICE"; 
        
        $pdf->SetFont('helvetica', 'B', 13);
        $style = array('width' => 0.2, 'cap' => 'butt', 'join' => 'miter', 'dash' => '10,20,5,10', 'phase' => 10, 'color' => array(0, 0, 0));
        $style3 = array('width' => 1, 'cap' => 'butt', 'join' => 'butt', 'dash' => 0, 'color' => array(0, 0, 0));
        
        $y=$pdf->getY();
        $x=7;
        $x2=$x+50;  
        $style2 = array('width' => 0.3, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0));
        $pdf->Line($x, $y, $x2, $y,$style2);
        $pdf->Line($x2, $y, $x2, $y+6, $style2);
        $pdf->Line($x2+112, $y, $x2+112, $y+6, $style2);
        $pdf->Line($x2, $y+6, $x2+112, $y+6, $style2);
        $pdf->Line($x2+112, $y, $x2+145, $y, $style2);
        
        $pdf->SetY($y_axis+1);
        $pdf->SetX($x_axis_initial);
        $pdf->SetFont('helvetica', ' ', 11);
        $pdf->Cell(40, 5, 'No : '.$invoice, 0, 0, 'L', 1); 
        
        $pdf->SetFont('helvetica', 'B', 13);
        $pdf->SetY($y_axis+1);
        $pdf->SetX($x_axis_initial+165);
        $pdf->Cell(30, 5, $appr, 0, 0, 'L', 1); 
        
        $pdf->SetLineStyle($style4);
        $y=$pdf->getY()+18;
        $x=7;
        $x2=$x+22;
        $pdf->Line($x, $y, $x2, $y);
        $pdf->Line($x2, $y, $x2, $y-5);
        $pdf->Line($x2, $y, $x2, $y-5);
        $pdf->Line($x2, $y-5, $x2+70, $y-5);
        
        $y_axis=$y_axis+13;
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial); 	
        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->MultiCell(22, 7, 'Pelanggan', 0, 'C', 0, 0, '', '', true);
        
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial+117); 	
        $pdf->MultiCell(18, 7, 'Ket.', 0, 'C', 0, 0, '', '', true);
        $x=$x_axis_initial+117;
        $x2=$x+18;
        $pdf->Line($x, $y, $x2, $y, $style4);
        $pdf->Line($x2, $y, $x2, $y-5, $style4);
        $pdf->Line($x2, $y, $x2, $y-5, $style4);
        $pdf->Line($x2, $y-5, $x2+60, $y-5, $style4);
        
        $pdf->SetFont('helvetica', ' ', 10); 
        $y_axis=$y_axis+9;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(22, 5, 'No. ID', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(70, 5, $id, 0, 'L', 0, 0, '', '', true);
        $yc=$pdf->getY()+5;
        $pdf->SetDrawColor(200, 200, 200);
        $pdf->Line(30, $yc, 99, $yc, $style7);
        
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial+117); 	
        $pdf->MultiCell(18, 5, 'Tanggal', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(60, 5, indonesian_longDate($inv_date), 0, 'L', 0, 0, '', '', true);
        $pdf->SetDrawColor(200, 200, 200);
        $xc=$pdf->getX();
        $pdf->Line($xc, $yc, $xc-59, $yc, $style7);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(22, 5, 'Nama', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(70, 5, $customer, 0, 'L', 0, 0, '', '', true);
        $yc=$pdf->getY()+5; $yc0=$yc+5;
        $pdf->SetDrawColor(200, 200, 200);
        $pdf->Line(30, $yc, 99, $yc, $style7);
        
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+117); 	
        $pdf->MultiCell(18, 5, 'Ref.', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(60, 5, $number, 0, 'L', 0, 0, '', '', true);
        $pdf->SetDrawColor(200, 200, 200);
        $xc=$pdf->getX();
        $pdf->Line($xc, $yc, $xc-59, $yc, $style7);
        
        
        $y_ax=$y_axis+5;
        if(strlen($customer) >= 35):
        	$y_axis=$y_axis+10;
        	$pdf->SetDrawColor(200, 200, 200);
        	$pdf->Line(30, $yc0, 99, $yc0, $style7);
        else:
        	$y_axis=$y_axis+5;
        endif;
        
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(22, 5, 'Alamat', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(70, 5, $addr1, 0, 'L', 0, 0, '', '', true);
        $yc=$pdf->getY()+5; 
        $pdf->SetDrawColor(200, 200, 200);
        $pdf->Line(30, $yc, 99, $yc, $style7);
        
        $pdf->SetY($y_ax); 
        $pdf->SetX($x_axis_initial+117); 	
        $pdf->MultiCell(18, 5, ' ', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(60, 5, indonesian_longDate($date),0, 'L', 0, 0, '', '', true);
        $pdf->SetDrawColor(200, 200, 200);
        $xc=$pdf->getX();
        $pdf->Line($xc, $yc-5, $xc-59, $yc-5, $style7);
        
        if($addr2!=""):
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        	$pdf->MultiCell(22, 5, ' ', 0, 'L', 0, 0, '', '', true);
        	$pdf->MultiCell(70, 5, $addr2, 0, 'L', 0, 0, '', '', true);	 
        	$yc=$pdf->getY()+5; 
        	$pdf->SetDrawColor(200, 200, 200);
        	$pdf->Line(30, $yc, 99, $yc, $style7);
        endif;
        
        $pdf->SetY($y_ax+5);
        $pdf->SetX($x_axis_initial+117); 	
        $pdf->MultiCell(18, 5, ' ', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(60, 5, $ref_1, 0, 'L', 0, 0, '', '', true);
        $pdf->SetDrawColor(200, 200, 200);
        $xc=$pdf->getX();
        $yc=$pdf->getY()+5;
        $pdf->Line($xc, $yc-5, $xc-59, $yc-5, $style7);
        
        if($addr3!=""):
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        	$pdf->MultiCell(22, 5, ' ', 0, 'L', 0, 0, '', '', true);
        	$pdf->MultiCell(70, 5, $addr3, 0, 'L', 0, 0, '', '', true);	 
        	$yc=$pdf->getY()+5; 
        	$pdf->SetDrawColor(200, 200, 200);
        	$pdf->Line(30, $yc, 99, $yc, $style7);
       	endif;
        
        $pdf->SetY($y_ax+10);
        $pdf->SetX($x_axis_initial+117); 	
        $pdf->MultiCell(18, 5, ' ', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(60, 5, $ref_2, 0, 'L', 0, 0, '', '', true);
        $pdf->SetDrawColor(200, 200, 200);
        $xc=$pdf->getX();
        $yc=$pdf->getY()+5;
        $pdf->Line($xc, $yc-5, $xc-59, $yc-5, $style7);
        $xc=$pdf->getX(); 
        $yc=$pdf->getY()+10;
        $pdf->Line($xc, $yc-5, $xc-59, $yc-5, $style7);
	   
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(22, 5, 'Telpon', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(70, 5, $telephone.'      Fax. '.$fax, 0, 'L', 0, 0, '', '', true);	 	
        $yc=$pdf->getY()+5; 
        $pdf->SetDrawColor(200, 200, 200);
        $pdf->Line(30, $yc, 99, $yc, $style7);
        
        if($curr=="IDR"):
        	$cur="(Rp.)";
        	$tar="Rp. /M";
        elseif($curr=="USD"):
        	$cur="(US$)";
        	$tar="USD/MMBTU";
        endif; 
        
        
        $pdf->SetLineStyle($style2);
        $pdf->SetFont('helvetica', 'B', 11);
        $y_axis=$y_axis+9;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->Cell(74, 6, 'Kedudukan Meter (M'.$pdf->unichr(179).')', 1, 1, 'C', 1, '', 0, false, 'T', 'C');
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+74);
        $pdf->Cell(43, 6, 'Pemakaian', 1, 1, 'C', 1, '', 0, false, 'T', 'C');
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+117);
        $pdf->Cell(18, 12, 'Cur.', 1, 1, 'C', 1, '', 0, false, 'T', 'C');
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+135);
        $pdf->Cell(25, 12, 'Tarif', 1, 1, 'C', 1, '', 0, false, 'T', 'C');
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+160);
        $pdf->Cell(35, 12, 'Jumlah '.$cur, 1, 1, 'C', 1, '', 0, false, 'T', 'C');
        
        $y_axis=$y_axis+6;
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial); 	
        $pdf->Cell(74, 6, 'Periode', 1, 1, 'C', 1, '', 0, false, 'T', 'C');
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+74);
        $pdf->Cell(43, 6, 'M'.$pdf->unichr(179), 1, 1, 'C', 1, '', 0, false, 'T', 'C');
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+117);
        
        $pdf->SetFont('helvetica', ' ', 10); 
        $y0=$y_axis+6;
        $y_axis=$y_axis+6;	
        
        /** jumlah Metering **/
        foreach($metering as $mtr):
            $pdf->SetLineStyle($style2);	
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        	$pdf->MultiCell(74, 10, $mtr['ItemLine'], 1, 'C', 0, 0, '', '', true);
            
            $use_gas = number_format($mtr['Pemakaian'],0,".",",");
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+74); 	
        	$pdf->Cell(43, 10, $use_gas.' ', 1, 1, 'R', 1, '', 0, false, 'T', 'C');
            
            $f_mtr   = number_format($mtr['Meteran_Awal'],0,".",",");
	        $l_mtr   = number_format($mtr['Meteran_Akhir'],0,".",",");
            
            $y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        	$pdf->MultiCell(37, 5, $f_mtr.' ', 0, 'R', 0, 0, '', '', true);
        	$pdf->MultiCell(37, 5, $l_mtr.' ', 0, 'R', 0, 0, '', '', true);
        	$yc=$pdf->getY();
            $yx=$pdf->getX();  
        	$pdf->SetDrawColor(200, 200, 200);
        	$pdf->Line(7, $yc, 81, $yc, $style7);
        	$pdf->Line(44, $yc, 44, $yc+5, $style7);
	        $y_axis=$y_axis+5;
            $periode = $mtr['ItemLine'];
        endforeach;
        
        for($m=COUNT($metering);$m<5;$m++):
	        $pdf->SetLineStyle($style2);
        	$pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial); 	
        	$pdf->MultiCell(74, 10, '', 1, 'C', 0, 0, '', '', true);
        	$pdf->MultiCell(43, 10, '', 1, 'R', 0, 0, '', '', true);
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        	$yc=$pdf->getY();
            $yx=$pdf->getX();  
        	$pdf->SetDrawColor(200, 200, 200);
        	$pdf->Line(7, $yc, 81, $yc, $style7);
        	$pdf->Line(44, $yc, 44, $yc+5, $style7);
        	$y_axis=$y_axis+5;
        endfor;
        
        $total     = $t_use;
        $total_gas = number_format($total['jml_pemakaian'],0,".",",");
        $avg       = $avg;
        $average   = $avg['rata'];
        
        if($average!=""):
        	$xxx =$average * 35.31466672;
        	$mmbtu0=1000000/$xxx;
        	$mmbtu=number_format($mmbtu0,4,".",",");	
        endif;	
        
        $pdf->SetLineStyle($style2);
        $pdf->SetFont('helvetica', 'B', 10);
        $y1=$y_axis;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+74); 	
        $pdf->MultiCell(43, 5, $total_gas.' ', 1, 'R', 0, 0, '', '', true);
        
        $pdf->SetY($y0); $pdf->SetX($x_axis_initial+117); 	
        $pdf->MultiCell(18, 60, '', 1, 'C', 0, 0, '', '', true);
        $pdf->MultiCell(25, 60, '', 1, 'C', 0, 0, '', '', true);
        $pdf->MultiCell(35, 60, '', 1, 'C', 0, 0, '', '', true);
        
        $pdf->SetFont('helvetica', ' ', 10);	
        $pdf->SetY($y1); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(117, 55, '', 1, 'C', 0, 0, '', '', true);
        $xd=$x_axis_initial+140;
        if($curr=="IDR")
        	$pdf->Text($xd, $y_axis+5,$tar.$pdf->unichr(179));
        else
        	$pdf->Text($xd-4, $y_axis+5,$tar);
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(115, 5, '   - Periode : Tgl.  '.$periode, 0, 'L', 0, 0, '', '', true);
        
        $no_m=1;
        $line = $line;
        foreach($line as $line):
            if ($curr['USD']) $convertion=$line['conversion'];
            $y_axis=$y_axis+5;
            $pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial+5); 	
		
            $jml_ket=strlen($line['Keterangan']);
            if ($curr['USD']) $n=30; else $n=35;
		    if($jml_ket > $n) $pdf->SetFont('helvetica', ' ', 7);	
		    else $pdf->SetFont('helvetica', ' ', 10);	
            
            if ($curr['USD']) $n=47; else $n=57;
            $pdf->MultiCell($n, 5, $line['Keterangan'], 0, 'L', 0, 0, '', '', true, 0, false, true, 5, 'M');
		
            $pdf->SetFont('helvetica', ' ', 10);	
            if ($curr['USD']) $n=17; else $n=25;
            $pdf->MultiCell($n, 5, number_format($line['pemakaian_m3'],0,".",","), 0, 'R', 0, 0, '', '', true, 0, false, true, 5, 'M');
		    if ($curr['USD']) $n=12; else $n=15;
            $pdf->MultiCell($n, 5, 'M'.$pdf->unichr(179).'  = ', 0, 'C', 0, 0, '', '', true, 0, false, true, 5, 'M');
            if($curr=='IDR'):
                $pdf->SetY($y_axis);
                $pdf->SetX($x_axis_initial+117); 	
                $pdf->MultiCell(18, 5, 'Rp.', 1, 'C', 0, 0, '', '', true, 0, false, true, 5, 'M');
            else:
                $pdf->MultiCell(18, 5, number_format($line['pemakaian_mmbtu'],2,".",","), 0, 'R', 0, 0, '', '', true, 0, false, true, 5, 'M');
        		$pdf->MultiCell(18, 5, 'MMBTU', 0, 'L', 0, 0, '', '', true, 0, false, true, 5, 'M');	
        		$pdf->MultiCell(18, 5, 'USD', 1, 'C', 0, 0, '', '', true, 0, false, true, 5, 'M');
            endif;
    		$pdf->MultiCell(25, 5, number_format($line['price'],2,".",","), 1, 'R', 0, 0, '', '', true, 0, false, true, 5, 'M');
    		$pdf->MultiCell(35, 5, number_format($line['jumlah'],2,".",","), 1, 'R', 0, 0, '', '', true, 0, false, true, 5, 'M');
            $no_m++;
        endforeach;
        
        
        if($curr=='IDR'):
            $pdf->SetFont('helvetica', 'B', 10);
    	    $height=35-(count($line) * 5);
    	
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial+135); 	
        	$pdf->MultiCell(25, 5, ' ', 1, 'C', 0, 0, '', '', true);
        	$pdf->MultiCell(35, 5, '', 1, 'R', 0, 0, '', '', true);
            
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+135); 	
        	$pdf->MultiCell(25, $height, 'Rp.', 1, 'C', 0, 0, '', '', true);
        	$pdf->MultiCell(35, 5, number_format($meter,0,".",","), 1, 'R', 0, 0, '', '', true);
        
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+135); 	
        	$pdf->MultiCell(25, $height, '', 1, 'C', 0, 0, '', '', true);
        	$pdf->MultiCell(35, $height, ' ', 1, 'C', 0, 0, '', '', true);
        else:
            $pdf->SetY($y_axis+10);
            $pdf->SetX($x_axis_initial+5); 	
	        $pdf->MultiCell(120, 5, 'Ket : 1 MMBTU = '.$convertion.' M'.$pdf->unichr(179), 0, 'L', 0, 0, '', '', true);
            $pdf->SetFont('helvetica', 'B', 10);
        	$height=35-(COUNT($line) *5 );
        	
            $y_axis=$y_axis+5;
        	$pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial+135); 
            $pdf->MultiCell(25, 5, ' ', 1, 'C', 0, 0, '', '', true);
	        $pdf->MultiCell(35, 5, '', 1, 'R', 0, 0, '', '', true);
            
            $y_axis=$y_axis+5;
        	$pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial+135); 	
        	$pdf->MultiCell(25, $height, 'USD', 1, 'C', 0, 0, '', '', true);
        	$pdf->MultiCell(35, 5, number_format($meter,2,".",","), 1, 'R', 0, 0, '', '', true);
            
            $y_axis=$y_axis+5;
        	$pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial+135); 	
        	$pdf->MultiCell(25, $height, '', 1, 'C', 0, 0, '', '', true);
        	$pdf->MultiCell(35, $height, ' ', 1, 'C', 0, 0, '', '', true);
        endif;
        $total_invoice=number_format($meter,0,".",",");
        
        $pdf->SetFont('helvetica', ' ', 10);
        $y_axis=$y_axis+$height-5;
        $pdf->SetY($y_axis); 
        $pdf->SetX($x_axis_initial);
        
        $total_word = strlen($calc);
        if($total_word >= 65):
        	$pdf->SetFont('helvetica', ' ', 10);
        	$pdf->Cell(160, 15, ' Terbilang ('.$curr.') : ', 1, 1, 'L', 1, '', 0, false, 'T', 'C');
        	
            $pdf->SetFont('helvetica', 'BI', 10);
            $pdf->SetY($y_axis+3);
            $pdf->SetX($x_axis_initial+32);
            $pdf->MultiCell(128, 15, $calc, 0, 'L', 0, 0, '', '', true); 
            		
        	$pdf->SetFont('helvetica', 'B', 10);
        	$pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial+160);
        	$pdf->Cell(35, 15, $total_invoice, 1, 1, 'R', 1, '', 0, false, 'T', 'C');
        	$y_axis=$y_axis+14;
        else:
        	$pdf->SetFont('helvetica', ' ', 10);
        	$pdf->Cell(160, 12, ' ', 1, 1, 'L', 1, '', 0, false, 'T', 'C');
        	$xd=$pdf->getX()-1;
        	$pdf->Text($xd, $y_axis+4, ' Terbilang ('.$curr.')   :  ');
        	$pdf->SetFont('helvetica', 'BI', 10);
        	$pdf->Text($xd+32, $y_axis+4, $calc); 		
        	$pdf->SetFont('helvetica', 'B', 10);
        	$pdf->SetY($y_axis);
            $pdf->SetX($x_axis_initial+160);
        	$pdf->Cell(35, 12, $total_invoice, 1, 1, 'R', 1, '', 0, false, 'T', 'C');
        	$y_axis=$y_axis+12;
        endif;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial);
        $pdf->Cell(195, 48, '', 1, 1, 'R', 1, '', 0, false, 'T', 'C');
        
        $y_axis=$y_axis+2;
        $pdf->SetFont('helvetica', ' ', 10);
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        $pdf->MultiCell(130, 5, 'Pembayaran Transfer ke Rekening', 0, 'L', 0, 0, '', '', true); 
        
        $pdf->SetFont('helvetica', 'I', 10);
        $pdf->MultiCell(55, 5, 'Hormat Kami,', 0, 'C', 0, 0, '', '', true); 		
        $pdf->SetFont('helvetica', ' ', 10);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        $pdf->MultiCell(30, 5, 'Nama', 0, 'L', 0, 0, '', '', true); 		
        $pdf->MultiCell(110, 5, ': PT. Bayu Buana Gemilang', 0, 'L', 0, 0, '', '', true); 		
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        $pdf->MultiCell(30, 5, 'Bank', 0, 'L', 0, 0, '', '', true); 		
        $pdf->MultiCell(110, 5, ': MUAMALAT Cab. Arthaloka', 0, 'L', 0, 0, '', '', true);
        
        if($curr=="USD"):
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        	$pdf->MultiCell(30, 5, 'Swift Code', 0, 'L', 0, 0, '', '', true); 		
        	$pdf->MultiCell(110, 5, ': MUABIDJA', 0, 'L', 0, 0, '', '', true); 		
        endif;
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial+3);
        $pdf->MultiCell(30, 5, 'No. ACC', 0, 'L', 0, 0, '', '', true); 		
        if($curr=="IDR")
        	$pdf->MultiCell(110, 5, ': 301-01707-10 (IDR)', 0, 'L', 0, 0, '', '', true); 		
        else
        	$pdf->MultiCell(110, 5, ': 301-00314-51 (USD)', 0, 'L', 0, 0, '', '', true); 
            
        $y_axis=$y_axis+9;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        $pdf->MultiCell(133, 5, 'Catatan : ', 0, 'L', 0, 0, '', '', true); 	
        $pdf->SetFont('helvetica', 'BIU', 10);
        $pdf->MultiCell(55, 5, $val['Pejabat'], 0, 'C', 0, 0, '', '', true); 		
        $pdf->SetFont('helvetica', ' ', 10);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        $pdf->MultiCell(131, 5, ' - Bukti transfer harap di fax ke PT. BBG, 021-51400149', 0, 'L', 0, 0, '', '', true); 
        $pdf->SetFont('helvetica', 'I', 10);
        $pdf->MultiCell(55, 5, $val['Status'], 0, 'C', 0, 0, '', '', true); 		
        $pdf->SetFont('helvetica', ' ', 10);
		
        $y_axis=$y_axis+5;
        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        $pdf->MultiCell(140, 5, ' - Pembayaran harap dilaksanakan selambatnya tanggal '.indonesian_longDate($val['DueDate']), 0, 'L', 0, 0, '', '', true); 	    			
                                
        $pdf->Output("Invoice.pdf","I");
    }
    
    function receipt(){
        $this->ws->set('title',lang('receipt'));/** title page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $this->ws->set('customers',$this->customers->getCustomerData()); /** table customer */
        if($this->session->userdata('kwt_month')):
            $month   =  $this->session->userdata('kwt_month');
            $year    =  $this->session->userdata('kwt_year');
            $cust    =  $this->session->userdata('kwt_cust');
            $curr    =  $this->session->userdata('kwt_curr');
        else:
            $month   = date('m');
            $year    = date('Y');
            $cust    = '-'; 
            $curr    = 'IDR'; 
        endif;
        $val = $this->invoices->getItemInvoice($cust,$month,$year); /** table invoice */
        $per = $this->invoices->getMeteringPeriode($cust,$month,$year); /** periode using gas **/
        if($val):
            if($curr=="IDR"):
            	$receipt = $val['NoKwiIDR'];
            	$meter   = $val['AmountIDR'];
            	$total   = 'Rp. '.number_format($val['AmountIDR'],0,".",",");
            	$calc    = IndonesianNumericToWord($meter);
            elseif($curr=="USD"):
            	$receipt = $val['NoKwiUSD'];
            	$meter   = $val['AmountUSD'];
            	$total   = 'US$. '.number_format($val['AmountUSD'],0,".",",");
            	$calc    = EnglishNumericToWord($meter);
            endif;
            $approval = $val['Approval'];
            $customer = $val['Perusahaan']; 
            $this->ws->set('approval',$approval); /** Approval **/
            $this->ws->set('receipt',$receipt); /** receipt no kwitansi IDR/USD field **/
            $this->ws->set('customer',$customer); /** Approval **/
            $this->ws->set('total',$total); /** Total Money **/
            $this->ws->set('calc',$calc); /** Total Calc Money **/
            $this->ws->set('item_line',$per['ItemLine']); /** Total Calc Money **/
        endif;
        $this->ws->set('month',$month); /** month session **/
        $this->ws->set('year',$year); /** year session **/
        $this->ws->set('cust',$cust); /** customer session **/
        $this->ws->set('curr',$curr); /** currency session **/
        $this->ws->set_theme('smooth'); /** set theme to smooth **/
		$this->ws->view('printing/receipt'); /** view layout to views/printing/receipt **/
    }
    
    function getReceipt(){
        /** post variabel**/
        $month   =  $this->input->post('month');
        $year    =  $this->input->post('year');
        $cust    =  $this->input->post('customer');
        $curr    =  $this->input->post('curr');
        $value   =  array('kwt_month'=>$month,'kwt_year'=>$year,'kwt_cust'=>$cust,'kwt_curr'=>$curr); /** insert post to array value **/
        $this->session->set_userdata($value); /** insert array of value to session **/
        redirect('billing-ar/printing/receipt',301); /** redirect to receipt function **/
    }    
    
    function getPdfReceipt(){
        /** session variabel**/
        $month   =  $this->session->userdata('kwt_month');
        $year    =  $this->session->userdata('kwt_year');
        $cust    =  $this->session->userdata('kwt_cust');
        $curr    =  $this->session->userdata('kwt_curr');
        $val = $this->invoices->getItemInvoice($cust,$month,$year); /** table invoice */
        $per = $this->invoices->getMeteringPeriode($cust,$month,$year); /** periode using gas **/
        $pdf = tcpdf();
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $k=0;
        $pdf->AddPage(); 
        
        /** initialization of x & y axis **/
        $x_axis_initial = 10;
        $y_axis_initial = 37;
        $x_axis = $x_axis_initial;
        $y_axis = $y_axis_initial;
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetDrawColor(0, 0, 0);
        $pdf->SetLineWidth(0.3); 

        $style4 = array('L' => 0,
                        'T' => array('width' => 0.25, 'cap' => 'butt', 'join' => 'miter', 'dash' => '20,10', 'phase' => 10, 'color' => array(100, 100, 255)),
                        'R' => array('width' => 0.50, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => array(50, 50, 127)),
                        'B' => array('width' => 0.75, 'cap' => 'square', 'join' => 'miter', 'dash' => '30,10,5,10'));
        $style2 = array('width' => 0.7, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0));
        $y=$pdf->getY()+18;
        $x=7;
        $x2=$x+22; 
        $pdf->SetLineWidth(0.3); 

        $y_axis0=$y_axis+11; 
        $y_axis=$y_axis;
        $pdf->SetY($y_axis0);
        $pdf->SetX($x_axis_initial); 	
        $pdf->Cell(190, 75, ' ', 1, 0, 'L', 1); 
        
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial+4);
        $pdf->SetFont('helvetica', 'B', 16);
        if($val['Approval']==0)
        	$pdf->Cell(50, 5, '    DRAFT', 0, 0, 'L', 1); 
        else $pdf->Cell(50, 5, '   KWITANSI', 0, 0, 'L', 1);
        
        $pdf->Line($x_axis_initial, $y+6, $x_axis_initial+55, $y+6, $style2);
        $pdf->Line($x_axis_initial+55, $y+6, $x_axis_initial+55, $y+16, $style2);
        $pdf->Line($x_axis_initial+55, $y+16, $x_axis_initial+140, $y+16, $style2);
        $pdf->Line($x_axis_initial+140, $y+6, $x_axis_initial+140, $y+16, $style2);
        $pdf->Line($x_axis_initial+140, $y+6, $x_axis_initial+190, $y+6, $style2); 
        
        $pdf->SetFont('helvetica', '', 10);
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial+141);
        $pdf->Cell(45, 5, 'No : '.$val['NoKwiIDR'], 0, 0, 'R', 1); 
        $pdf->SetLineWidth(0.3); 
        
        $y_axis=$y_axis+14;
        $pdf->SetFont('helvetica', 'B', 12);
        $pdf->Ln(); 
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3); 	
        $pdf->Cell(70, 7, 'Telah Terima Dari', 0, 0, 'L', 1);
        $pdf->Cell(5, 7, ':', 0, 0, 'C', 1);
        $pdf->MultiCell(110, 7, $val['Perusahaan'], 1, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+7;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3); 	
        $pdf->Cell(70, 7, 'Sejumlah', 0, 0, 'L', 1);
        $pdf->Cell(5, 7, ':', 0, 0, 'C', 1);
        $pdf->SetFont('helvetica', 'I', 12);
        
        if($curr=="IDR")
            $terbilang = IndonesianNumericToWord($val['AmountIDR']).' Rupiah';
        else 
            $terbilang =EnglishNumericToWord($val['AmountUSD']);
        $pdf->MultiCell(110, 7, $terbilang, 1, 'L', 0, 0, '', '', true);
        $pdf->Ln();
        
        $y_axis=$y_axis+28;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+3);
        $pdf->SetFont('helvetica', 'B', 12);
        
        $pdf->Cell(70, 7, 'Untuk Pembayaran ', 0, 0, 'L', 1);
        $pdf->Cell(5, 7, ':', 0, 0, 'C', 1);
        $pdf->SetFont('helvetica', '', 12);
        $pdf->MultiCell(110, 7, 'Pemakaian Gas Periode tgl '.$per['ItemLine'], 1, 'L', 0, 0, '', '', true);
        $pdf->Ln();
        
        $y_axis=$y_axis+24;
        $pdf->SetFont('helvetica', 'B', 12);
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+12);
        if($curr=="IDR")
        	$pdf->Cell(50, 7, 'Rp. '.decimal($val['AmountIDR'],2).'.-', 1, 0, 'L', 1);
        else
        	$pdf->Cell(50, 7, 'US$. '.decimal($val['AmountUSD'],2), 1, 0, 'L', 1);
        
        $pdf->SetY($y_axis+20); $pdf->SetX($x_axis_initial); 	
        $pdf->Cell(190, 50, ' ', 1, 0, 'L', 1);  
          
        $y_axis=$y_axis+20;
        $pdf->SetFont('helvetica', 'I', 12);
        
        $pdf->SetY($y_axis+2); $pdf->SetX($x_axis_initial+105);
        $pdf->Cell(80, 7, ' Jakarta, '.$val['InvoiceDate'], 0, 0, 'C', 1);
        $y_axis=$y_axis+35;
        
        $pdf->SetY($y_axis);
        $pdf->SetX($x_axis_initial+105);
        $pdf->SetFont('helvetica', 'BUI', 12);
        $pdf->Cell(80, 6, $val['Pejabat'], 0, 0, 'C', 1);
        
        $pdf->SetFont('helvetica', 'BI', 12);
        $pdf->SetY($y_axis+5); $pdf->SetX($x_axis_initial+105);
        $pdf->Cell(80, 5, $val['Status'], 0, 0, 'C', 1);
        $pdf->Output("Kwitansi.pdf","I");
           
    }
    
    function letter(){
        $this->ws->set('title',lang('covering_letter'));/** title page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $this->ws->set('customers',$this->customers->getCustomerData()); /** table customer */
        $this->ws->set_theme('smooth'); /** set theme to smooth*/
		$this->ws->view('printing/letter'); /** view layout to views/printing/letter*/
    }
    
    function getPrintingLetter(){
        /** variabel post **/
        $month    =  $this->input->post('month');
        $year     =  $this->input->post('year');
        $curr     =  $this->input->post('curr');
        $cust     =  $this->input->post('customer');
        $today    =  date('Y-m-d');
        
        if(!$this->invoices->getItemInvoice($cust,$month,$year)):
            $this->session->set_flashdata('error','error');    
            redirect('billing-ar/printing/letter',301);
        endif;
        $invoice = $this->invoices->getItemInvoice($cust,$month,$year); 
        
        if($curr=="IDR"):
        	$no     = $invoice['NoPengIDR'];
        	$inv_no = $invoice['NoInvIDR'];
        	$kwit   = $invoice['NoKwiIDR'];
        	$meter  = $invoice['AmountIDR'];
            $calc   = IndonesianNumericToWord($meter).' Rupiah';
        	$meter  = number_format($meter,0,".",",");
            $total  = 'Rp. '.$meter;
        else:
            $no     = $invoice['NoPengUSD'];
        	$inv_no = $invoice['NoInvUSD'];
        	$kwit   = $invoice['NoKwiUSD'];
        	$meter  = $invoice['AmountUSD'];
            $calc   = EnglishNumericToWord($meter);
        	$meter  = number_format($meter,0,".",",");
            $total  = 'US$. '.$meter;
        endif;
        $customer   = $invoice['Perusahaan'];    
        $addr1      = $invoice['Alamat_1'];  
        $addr2      = $invoice['Alamat_2'];
        $addr3      = $invoice['Alamat_3'];
        $metering   = $this->invoices->getMeteringPeriode($cust,$month,$year);
        $due_desc   = $metering['ItemLine'];
        $due_date   = indonesian_longDate($invoice['DueDate']);
        $auth       = $invoice['Pejabat'];
        $auth_status= $invoice['Status'];
        $inv_date   = indonesian_longDate($invoice['InvoiceDate']);  
        $this->getPdfLetter($no,$customer,$addr1,$addr2,$addr3,$due_desc,$total,$calc,$due_date,$kwit,$inv_no,$inv_date,$auth,$auth_status); 
        $this->session->set_flashdata('success','success');    
        redirect('billing-ar/printing/letter',301);      
    }
    
    function getPdfLetter($no,$cust,$addr1,$addr2='',$addr3='',$due,$total='',$calc='',$due_date,$kwit,$inv_no,$inv_date,$auth,$auth_status){
        $pdf = tcpdf();
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $k=0;
        $pdf->AddPage(); 
        /** initialization of x & y axis **/
        $x_axis_initial = 15;
        $y_axis_initial = 35;
        $x_axis = $x_axis_initial;
        $y_axis = $y_axis_initial; 
        $pdf->SetFont('helvetica', '', 14);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetDrawColor(0, 0, 0);
        $pdf->SetLineWidth(0.3);
        $y_axis0=$y_axis+7; 
        $y_axis=$y_axis;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial);
        $pdf->setFontSubsetting(false);
        
        /** No **/
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->SetFont('helvetica', ' ', 11);
        $pdf->MultiCell(175, 7, 'No : '.$no, 0, 'L', 0, 0, '', '', true);
        
        /** customer **/
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->SetFont('helvetica', 'B', 11);
        $pdf->MultiCell(175, 5, $cust, 0, 'L', 0, 0, '', '', true);
        
        /** address I **/
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->SetFont('helvetica', ' ', 11);
        $pdf->MultiCell(175, 5, $addr1, 0, 'L', 0, 0, '', '', true);
        
        /** address II **/
        if($addr2!=""):
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        	$pdf->MultiCell(175, 5, $addr2, 0, 'L', 0, 0, '', '', true);
        endif;
        
        /** address III **/
        if($addr3!=""):
        	$y_axis=$y_axis+5;
        	$pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        	$pdf->MultiCell(175, 5, $addr3, 0, 'L', 0, 0, '', '', true);
        endif;
        
        /** Gas Debt Invoice **/
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->SetFont('helvetica', 'B', 11);
        $pdf->MultiCell(20, 5, 'Perihal :  ', 0, 'L', 0, 0, '', '', true);
        $pdf->SetFont('helvetica', 'BU', 11);
        $pdf->MultiCell(150, 5, 'Tagihan Pemakaian Gas dari Tgl. '.$due, 0, 'L', 0, 0, '', '', true);
        
        /** Gas Debt Invoice Details **/
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->SetFont('helvetica', ' ', 11);
        $pdf->MultiCell(175, 5, 'Dengan hormat, ', 0, 'L', 0, 0, '', '', true);
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'Sehubungan dengan perhitungan dari Pemakaian Gas dari Tgl. '.$due.' yang telah disepakati, maka bersama ini kami mengajukan tagihan pemakaian tersebut sebesar : ', 0, 'L', 0, 0, '', '', true);
        
         /** Gas Debt Invoice Total Payment **/
        $y_axis=$y_axis+15;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
       	$pdf->MultiCell(45, 5,$total, 0, 'L', 0, 0, '', '', true);
       	$pdf->MultiCell(125, 5,'( '.$calc.' )', 0, 'L', 0, 0, '', '', true);
        
        /** Gas Debt Due Date **/
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(94, 5, 'Pembayaran dilaksanakan selambat-lambatnya pada ', 0, 'L', 0, 0, '', '', true);
        $pdf->SetFont('helvetica', 'B', 12);
        $pdf->MultiCell(82, 5, 'Tanggal '.$due_date, 0, 'L', 0, 0, '', '', true);
        
        $pdf->SetFont('helvetica', ' ', 11);
        
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'Sebagai data pendukung kami lampirkan :', 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+5); 	
        $pdf->MultiCell(40, 5, '1.  Kwitansi No. ', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(135, 5,$kwit, 0, 'L', 0, 0, '', '', true);  
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+5); 	
        $pdf->MultiCell(40, 5, '2.  Invoice Billing No . ', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(135, 5,$inv_no, 0, 'L', 0, 0, '', '', true);      
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial+5); 	
        $pdf->MultiCell(175, 5, '3.  Dan Dokumen pendukung lainnya.', 0, 'L', 0, 0, '', '', true);

        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'Pembayaran di transfer ke :', 0, 'L', 0, 0, '', '', true);

        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'PT. Bayu Buana Gemilang', 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'Bank Muamalat Indonesia Cab. Arthaloka', 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(30, 5, 'Swift code', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(140, 5, ' : MUABIDJA', 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(30, 5, 'No. Acc.', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(140, 5, ' : 301-00314-51 (USD)', 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(30, 5, 'No. Acc.', 0, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(140, 5, ' : 301-01707-10 (IDR)', 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+10;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'Demikianlah permohonan kami sampaikan, atas perhatian dan kerjasamanya kami sampaikan terima kasih.', 0, 'L', 0, 0, '', '', true);

        $y_axis=$y_axis+17;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'Hormat kami,', 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'Jakarta, '.$inv_date, 0, 'L', 0, 0, '', '', true);
        
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, 'PT. BAYU BUANA GEMILANG', 0, 'L', 0, 0, '', '', true);

        $pdf->SetFont('helvetica', 'BU', 12);
        $y_axis=$y_axis+25;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, $auth, 0, 'L', 0, 0, '', '', true);
        
        $pdf->SetFont('helvetica', ' ', 12);
        $y_axis=$y_axis+5;
        $pdf->SetY($y_axis); $pdf->SetX($x_axis_initial); 	
        $pdf->MultiCell(175, 5, $auth_status, 0, 'L', 0, 0, '', '', true);

        $pdf->Output("Surat Pengantar.pdf","I");
    }
    
    function gas_consumption(){
        $this->ws->set('title',lang('gas_consumption'));/** title page Gas Consumption */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        /** if session already or no to view data **/
        if($this->session->userdata('gc_month')):
            $month =   $this->session->userdata('gc_month');
            $year  =   $this->session->userdata('gc_year');
            $cust  =   $this->session->userdata('gc_customer');
        else:
            $month =   '-';
            $year  =   '-';
            $cust  =   '-';
        endif;
        $invoice = $this->invoices->getItemInvoice($cust,$month,$year);
        if($invoice)
             $invoiceID = $invoice['InvoiceID'];
        else $invoiceID = '-';       
        $pjbg    = $this->invoices->getPjbg($invoiceID);
        if($pjbg)
             $pjbgID = $pjbg['pjbg_id'];
        else $pjbgID = '-';    
        $ghv     = $this->invoices->getGhv($invoiceID);
        $metering= $this->invoices->getMetering($cust,$month,$year,$pjbgID);     
        $customer= $this->customers->getCustomer($cust); 
        $line    = $this->invoices->getMeteringLine('USD',$pjbgID,$invoiceID);
        $line2   = $this->invoices->getMeteringLine('IDR',$pjbgID,$invoiceID);
        
        $this->ws->set('month',$month); /** variabel month from session **/
        $this->ws->set('year',$year); /** variabel year from session **/
        $this->ws->set('customer',$cust); /** variabel customer from session **/
        $this->ws->set('customers',$this->customers->getCustomerData()); /** Get Data Table Customers */
        $this->ws->set('pjbg',$pjbg); /** get data form item_pjbg **/
        $this->ws->set('ghv',$ghv); /** get data form ghv **/
        $this->ws->set('metering',$metering); /** get data form Metering **/
        $this->ws->set('line',$line); /** get data form Line **/
        $this->ws->set('line2',$line2); /** get data form Line **/
        $this->ws->set('customerData',$customer);/** Get Customer Data **/
        $this->ws->set_theme('smooth'); /** set theme to smooth **/
		$this->ws->view('printing/gas_consumption'); /** view layout to views/printing/gas_consumption **/
    }
    
    function getGasConsumption(){
        /** array : insert value session from post **/
        $value  = array('gc_month'     => $this->input->post('month'),
                        'gc_year'      => $this->input->post('year'),
                        'gc_customer'  => $this->input->post('customer'));
        $this->session->set_userdata($value); /** set userdata value **/
        redirect('billing-ar/printing/gas_consumption',301); /** redirect to gas Consumption **/
    }
    
    function getGasConsumptionExport(){  
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('billing-ar/printing/getGasConsumptionExcel/'); 
        /** if export via pdf **/    
        elseif($this->input->post('export')=='pdf')    
            redirect('billing-ar/printing/getGasConsumptionPdf/');
        /** if export via email **/  
        elseif($this->input->post('export')=='email')    
            redirect('billing-ar/printing/getGasConsumptionEmail');
		else
			echo 'Error redirecting'; /** if error redirecting the page **/
    }
    
    function getGasConsumptionPdf(){
        /** Session Variabel **/
        $month =   $this->session->userdata('gc_month');
        $year  =   $this->session->userdata('gc_year');
        $cust  =   $this->session->userdata('gc_customer');
        /** go pdf **/
       	$pdf          =   $this->gas->getPdfGasConsumption($month,$year,$cust); /** get Pdf File **/
        $value        =   file_get_contents($pdf); /** Read the file's contents **/
        $name         =   'Gas-Consumption.pdf'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
    
    function getGasConsumptionReset(){
        /** array : insert value session from post **/
        $value  = array('gc_month'     => 'gc_month',
                        'gc_year'      => 'gc_year',
                        'gc_customer'  => 'gc_customer');
        $this->session->unset_userdata($value); /** unset userdata value **/
        redirect('billing-ar/printing/gas_consumption',301); /** redirect to gas Consumption **/
    }
    
}